package com.learn.redis.handler;

import com.xcdh.commons.spring.aspect.XCDHAspectInter;
import com.xcdh.commons.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class CloudSchedulerTask implements XCDHAspectInter {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
        String name = joinPoint.getSignature().getClass().getName();
        System.out.println("TIME:::" + DateUtils.getStringDate());
        Boolean absent = redisTemplate.opsForValue().setIfAbsent(name, "0", 3, TimeUnit.SECONDS);
        if(absent){// 没有其他服务执行
            log.info("任务调度===执行成功的线程==={}",Thread.currentThread().getName());
            return joinPoint.proceed();
        }else{
            log.info("任务调度XXXXX拒绝执行的线程XXXXX{}",Thread.currentThread().getName());
        }
        return null ;
    }
}
